package com.facedemo.utils;

import android.content.Context;
import android.content.SharedPreferences;

public class SharePreferencesUtils {
    public static String PREFERENCE_NAME = "sp_face";

    public static final String CAMERA_ID_SELECT = "CAMERA_ID_SELECT";

    /**
     * 存储字符串
     */
    public static boolean putString(Context context, String key, String value) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(key, value);
        return editor.commit();
    }

    /**
     * 读取字符串
     */
    public static String getString(Context context, String key) {
        return getString(context, key, null);
    }

    /**
     * 读取字符串（带默认值的）
     */
    public static String getString(Context context, String key, String defaultValue) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
        return preferences.getString(key, defaultValue);
    }

    /**
     * 存储整型数字
     */
    public static boolean putInt(Context context, String key, int value) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putInt(key, value);
        return editor.commit();
    }

    /**
     * 读取整型数字
     */
    public static int getInt(Context context, String key) {
        return getInt(context, key, -1);
    }

    /**
     * 读取整型数字（带默认值的）
     */
    public static int getInt(Context context, String key, int defaultValue) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
        return preferences.getInt(key, defaultValue);
    }

    /**
     * 存储长整型数字
     */
    public static boolean putLong(Context context, String key, long value) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putLong(key, value);
        return editor.commit();
    }

    /**
     * 读取长整型数字
     */
    public static long getLong(Context context, String key) {
        return getLong(context, key, 0xffffffff);
    }

    /**
     * 读取长整型数字（带默认值的）
     */
    public static long getLong(Context context, String key, long defaultValue) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
        return preferences.getLong(key, defaultValue);
    }

    /**
     * 存储Float数字
     */
    public static boolean putFloat(Context context, String key, float value) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putFloat(key, value);
        return editor.commit();
    }

    /**
     * 读取Float数字
     */
    public static float getFloat(Context context, String key) {
        return getFloat(context, key, -1.0f);
    }

    /**
     * 读取Float数字（带默认值的）
     */
    public static float getFloat(Context context, String key, float defaultValue) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
        return preferences.getFloat(key, defaultValue);
    }

    /**
     * 存储boolean类型数据
     */
    public static boolean putBoolean(Context context, String key, boolean value) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean(key, value);
        return editor.commit();
    }

    /**
     * 读取boolean类型数据
     */
    public static boolean getBoolean(Context context, String key) {
        return getBoolean(context, key, false);
    }

    /**
     * 读取boolean类型数据（带默认值的）
     */
    public static boolean getBoolean(Context context, String key, boolean defaultValue) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
        return preferences.getBoolean(key, defaultValue);
    }

//    /**
//     * 保存一个对象
//     * @param key      sp key值
//     * @param data 要保存的对象
//     * @param <T>  对象类型
//     */
//    public static <T> void saveDataObj(Context context, String key, T data) {
//        if (data == null) return;
//
//        Gson gson = new Gson();
//        //转换成json数据，再保存
//        String strJson = gson.toJson(data);
//
//        SharedPreferences sp = context.getSharedPreferences(PREFERENCE_NAME, 0);
//        SharedPreferences.Editor editor = sp.edit();
//        editor.putString(key, strJson);
//        editor.commit();
//    }
//
//    /**
//     * 获取对象
//     * @param key sp key值
//     * @param cls 对象类型
//     * @return list
//     */
//    public static <T> T getDataObj(Context context, String key, Class<T> cls) {
//        SharedPreferences sp = context.getSharedPreferences(PREFERENCE_NAME, 0);
//        String strJson = sp.getString(key, "");
//        if (null == strJson || strJson.length()==0) {
//            return null;
//        }
//        T obj = null;
//        try {
//            Gson gson = new Gson();
//            obj = gson.fromJson(strJson, cls);
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        return obj;
//    }

    /**
     * 删除一条信息
     */
    public static void delete(Context context, String key) {
        SharedPreferences sp = context.getSharedPreferences(PREFERENCE_NAME, 0);
        SharedPreferences.Editor editor = sp.edit();
        editor.remove(key);
        editor.commit();
    }


    /**
     * 清除数据
     */
    private static boolean clearPreferences(Context context) {
        SharedPreferences pref = context.getSharedPreferences(PREFERENCE_NAME, 0);
        SharedPreferences.Editor editor = pref.edit();
        editor.clear();
        return editor.commit();
    }
}